/*
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Initial Developer of the Original Code is
# Davide Ficano.
# Portions created by the Initial Developer are Copyright (C) 2007
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
#   Davide Ficano <davide.ficano@gmail.com>
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****
+ morenppkomodo: smd
*/
//xtk.include("domutils");

var moreNppKomodo = {

    onLoad : function() {
        window.controllers.appendController(this);
    },

    onUnLoad : function() {
        window.controllers.removeController(this);
    },

    onEvent : function(evt) {
    },
    
    supportsCommand : function(cmd) {
        switch (cmd) {
            case "cmd_morenppkomodo_dupl":
            case "cmd_morenppkomodo_mvup":
            case "cmd_morenppkomodo_mvdn":
                return true;
        }
        return false;
    },

    isCommandEnabled : function(cmd) {
        // at startup with no file open manager is null
        var view = ko.views.manager && ko.views.manager.currentView;

        switch (cmd) {
            case "cmd_morenppkomodo_dupl":
            case "cmd_morenppkomodo_mvup":
            case "cmd_morenppkomodo_mvdn":
                return view && view.getAttribute('type') == 'editor';
        }
        return false;
    },

    doCommand : function(cmd) {
        switch (cmd) {
            case "cmd_morenppkomodo_dupl":
                this.onDuplicateSelRow();
                break;
            case "cmd_morenppkomodo_mvup":
                this.onMoveRowUp();
                break;
            case "cmd_morenppkomodo_mvdn":
                this.onMoveRowDown();
                break;
        }
    },


    onDuplicateSelRow : function() {
        var currView = ko.views.manager.currentView;
        
        /* var viewDoc = currView.document;
        var oldName = viewDoc.file.baseName;
        //crashes on object 'komodo'
        var ke = ko.editor; //undefined
        alert(currView + "\r\n" + viewDoc + "\r\n"
            + scimoz + "\r\n" + sel + "\r\n" + scimoz.currentPos + "\r\n"
            + ko ); */
        
        //scimoz Attributes - http://docs.activestate.com/komodo/4.4/macroapi.html#macroapi_scimoz
        var scimoz = currView.scintilla.scimoz;
        var sel = currView.selection;

        if (sel.length) {
            // Copy the current selection
            var opos = scimoz.currentPos;
            var olen = sel.length;
            opos -= olen; 
            var pos = opos + olen;
            scimoz.insertText(pos,sel);
            //remake selection
            scimoz.setSel(opos, pos);
            
        }
        else
        {
            scimoz.lineDuplicate();
        }

    },

    //notepad++ currentLineUp
    onMoveRowUp : function() {
        var currView = ko.views.manager.currentView;
        var scimoz = currView.scintilla.scimoz;
        var opos = scimoz.currentPos;
        var olinenum = scimoz.lineFromPosition(opos);
        
        if (currView && currView.getAttribute('type') == 'editor') {
        if (olinenum != 0)
        {
            var nlinenum = olinenum - 1;
            scimoz.lineTranspose();
            scimoz.gotoLine(nlinenum);
        }
        }
    },

    //notepad++ currentLineDown
    onMoveRowDown : function() {
        var currView = ko.views.manager.currentView;
        var scimoz = currView.scintilla.scimoz;
        
        var opos = scimoz.currentPos;
        var olinenum = scimoz.lineFromPosition(opos);
        var lc = scimoz.lineCount;
        
        if (olinenum != lc - 1)
        {
            var nlinenum = olinenum + 1;
            scimoz.gotoLine(nlinenum);
            scimoz.lineTranspose();
        }
    }

}; 

window.addEventListener("load", function(event) { moreNppKomodo.onLoad(event); }, false);
window.addEventListener("unload", function(event) { moreNppKomodo.onUnLoad(event); }, false);
